package com.jichangxiu.system.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.vo.PageResult;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.system.entity.dto.add.AddSysRole;
import com.jichangxiu.system.entity.dto.edit.EditSysRole;
import com.jichangxiu.system.entity.dto.query.QuerySysRole;
import com.jichangxiu.system.entity.vo.SysRoleVo;
import com.jichangxiu.system.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * SysRoleAction:
 *
 * @author JiChangXiu
 * @create 2024-06-28 13:44:39
 */
@Slf4j
@Rest("/system/role")
@Tag(name = "系统角色控制器")
public class SysRoleAction extends BaseAction {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 新增系统角色
     *
     * @param addSysRole 新增系统角色视图对象
     * @return 是否成功
     */
    @Post
    @Limit
    @Repeat
    @Log(remark = "新增系统角色")
    @Operation(summary = "新增系统角色")
    @PreAuthorize("@jcx.hasPower('system:role:add')")
    public String add(@RequestBody AddSysRole addSysRole) {
        return str(sysRoleService.add(addSysRole));
    }

    /**
     * 单（多）选删除系统角色
     *
     * @param ids 系统角色主键列表
     * @return 是否成功
     */
    @Delete
    @Limit
    @Repeat
    @Log(remark = "单（多）选删除系统角色")
    @Operation(summary = "单（多）选删除系统角色")
    @PreAuthorize("@jcx.hasPower('system:role:del')")
    public String del(@RequestBody String[] ids) {
        return str(sysRoleService.del(ListUtil.toList(ids)));
    }

    /**
     * 修改系统角色
     *
     * @param editSysRole 修改系统角色视图对象
     * @return 是否成功
     */
    @Put
    @Limit
    @Repeat
    @Log(remark = "修改系统角色")
    @Operation(summary = "修改系统角色")
    @PreAuthorize("@jcx.hasPower('system:role:edit')")
    public String edit(@RequestBody EditSysRole editSysRole) {
        return str(sysRoleService.edit(editSysRole));
    }

    /**
     * 根据主键查询系统角色
     *
     * @param id       系统角色主键
     * @param isExtend 是否扩展
     * @return 系统角色详情
     */
    @Get("/{id}")
    @Operation(summary = "根据主键查询系统角色")
    @PreAuthorize("@jcx.hasPower('system:role:info')")
    public SysRoleVo info(@PathVariable("id") @NotBlank(message = "主键不能为空") String id, @RequestParam(value = "isExtend", required = false, defaultValue = "false") Boolean isExtend) {
        return sysRoleService.info(id, isExtend);
    }

    /**
     * （可分页）查询系统角色列表
     *
     * @param querySysRole 查询视图对象
     * @return （分页）系统角色列表
     */
    @Get("/lists")
    @Operation(summary = "（可分页）查询系统角色列表")
    @PreAuthorize("@jcx.hasPower('system:role:lists')")
    public PageResult lists(@ParameterObject QuerySysRole querySysRole) {
        return pageResult(sysRoleService.lists(querySysRole));
    }

}